# Contributor: Kreijstal <rainb@tfwno.gf>

_realname=ollama
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgdesc='Create, run and share large language models (LLMs) (mingw-w64)'
pkgver=0.7.1
pkgrel=1
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://ollama.com'
msys2_repository_url='https://github.com/ollama/ollama'
msys2_references=(
  'cpe: cpe:2.3:a:ollama:ollama'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             git)
optdepends=("${MINGW_PACKAGE_PREFIX}-openblas")
source=(https://github.com/ollama/ollama/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-no-static-linking.patch
        0002-fix-dynamic-libraries-path.patch)
sha256sums=('1cdf163d13a92f540f2da49949e60e613f187cd328bfdcfdfcc83d6bdd507902'
            '8dfbc740dab6b28e826cc038d321f836f034290aadf0f946e8230c2a4f57df26'
            'c028fb2f928f011e746852833aa1ba5cb80d23a67c8427cf9df84f9826bab0bd')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    0001-no-static-linking.patch \
    0002-fix-dynamic-libraries-path.patch

  if check_option "debug" "n"; then
    sed -i 's,var mode string = gin.DebugMode,var mode string = gin.ReleaseMode,g' server/routes.go
  fi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  if [[ ${CARCH} == aarch64 ]]; then
    # See https://github.com/ggerganov/llama.cpp/blob/325afb37/cmake/arm64-windows-llvm.cmake#L12
    local _flags="-march=armv8.7-a -fvectorize -ffp-model=fast -fno-finite-math-only"
    CFLAGS+=" ${_flags}"
    CXXFLAGS+=" ${_flags}"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DGGML_WIN_VER="0xA00" \
    -DGGML_OPENMP=ON \
    -DGGML_BLAS=ON \
    -DGGML_BLAS_VENDOR=OpenBLAS \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  export CGO_CFLAGS="$CFLAGS" CGO_CPPFLAGS="$CPPFLAGS" CGO_CXXFLAGS="$CXXFLAGS" CGO_LDFLAGS="$LDFLAGS"
  export GOPROXY=direct
  export GOROOT=${MINGW_PREFIX}/lib/go
  export GOPATH=${MINGW_PREFIX}/

  go build -ldflags "${LDFLAGS} -X=github.com/ollama/ollama/version.Version=${pkgver}" .
}

check() {
  $srcdir/build-$MSYSTEM/ollama --version >/dev/null

  cd "build-${MSYSTEM}"
  go test .
}

package() {
  cd $srcdir/build-$MSYSTEM

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  cp lib/ollama/libggml-blas.dll "${pkgdir}"${MINGW_PREFIX}/lib/ollama

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/bin
  install ollama.exe -t"${pkgdir}"${MINGW_PREFIX}/bin

  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
